package com.example.springbootswagger2.configuration;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.google.common.base.Predicates;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// @ConditionalOnProperty(name = "swagger2.enable", havingValue = "true") // 可以用这个spring注解来启动swagger（swagger2.enable配置成true时启用)
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@Profile({"dev", "test", "pre"})// 这里不添加"pro"，可以控制生产环境不开启swagger
public class Swagger2UiConfiguration extends WebMvcConfigurerAdapter {

    @Value("${swagger2.enable}")
    private boolean swagger2Enable;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 1、权限控制启用参数
                .enable(swagger2Enable)
                // 绑定swagger-ui的展示内容
                .apiInfo(new ApiInfoBuilder().title("swagger和springBoot整合演示").description("swagger的API文档演示效果").version("1.0").build())
                .select()
                // 绑定扫描的类
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
                .build()
                // 2、设置安全规则
                .securitySchemes(this.securitySchemes())
                // 3、设置哪些api应用安全规则
                .securityContexts(Collections.singletonList(SecurityContext.builder()
                        .securityReferences(this.defaultAuth()).forPaths(PathSelectors.any()).build()));
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    private List<SecurityScheme> securitySchemes() {
        List<SecurityScheme> list = new ArrayList<>();
        list.add(new BasicAuth("basicAuth"));
        list.add(new ApiKey("write_token", "write_token", "header"));
        list.add(new ApiKey("read_token", "read_token", "query"));
        return list;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Collections.singletonList(new SecurityReference("Authorization", authorizationScopes));
    }

}
